package com.jkd.controller;

import com.alibaba.fastjson.JSON;
import com.jkd.model.pojo.Room;
import com.jkd.model.pojo.StuRoomRelate;
import com.jkd.model.pojo.Student;
import com.jkd.model.vo.Result;
import com.jkd.service.IRoomService;
import com.jkd.service.IStuRoomRelateService;
import com.jkd.service.impl.RoomServiceImpl;
import com.jkd.service.impl.StuRoomRelateServiceImpl;
import com.mysql.cj.xdevapi.JsonString;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/service/doc/*")
public class DocController extends HttpServlet {
    private IRoomService roomService = new RoomServiceImpl();
    private IStuRoomRelateService iStuRoomRelateService = new StuRoomRelateServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        switch (pathInfo){
           case "/checkIn":
               resp.setContentType("application/json;charset=utf-8");
               PrintWriter out  = resp.getWriter();
               req.setCharacterEncoding("utf-8");
               String roomId = req.getParameter("roomId");
               Room  room = roomService.selectById(Integer.valueOf(roomId));

               Student student = (Student) req.getSession().getAttribute("student");
              Integer relateId = iStuRoomRelateService.selectByStuIdAndRoomId(student.getStuId());
//               System.out.println(stuRoomRelate1);
               Boolean flag = null;
               flag = false;
               if(relateId == null){
                  StuRoomRelate stuRoomRelate = new StuRoomRelate();
                  stuRoomRelate.setRoomId(room.getRoomId());
                  stuRoomRelate.setStuId(student.getStuId());
                  stuRoomRelate.setCheckInTime(new Date());
                  stuRoomRelate.setOther1(student.getName());
                  stuRoomRelate.setOther2(student.getStuNum());
                  stuRoomRelate.setDeleted(1);
                  flag = iStuRoomRelateService.insert(stuRoomRelate);
              }
               Result result;
               if(flag){
                   result = new Result("200","添加成功",null);
               }else {
                   result = new Result("500","添加失败",null);
               }
               out.print(JSON.toJSONString(result));
               break;

//            case"/table":
////
//                req.getRequestDispatcher("/WEB-INF/jsp/table.jsp").forward(req,resp);
//                break;

            case "/detail":
                String roomId1 = req.getParameter("roomId");
                System.out.println(roomId1+"===Y");
                List<StuRoomRelate> list = iStuRoomRelateService.selectByRoomId(Integer.valueOf(roomId1));
                list.forEach(e-> System.out.println(e));
                req.setAttribute("stuList",list);
                req.getRequestDispatcher("/WEB-INF/jsp/detail.jsp").forward(req,resp);
                break;
        }
    }
}
